package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackingListDao;
import cn.itcast.dao.cargo.ShippingOrderDao;
import cn.itcast.domain.cargo.PackingList;
import cn.itcast.domain.cargo.ShippingOrder;
import cn.itcast.domain.cargo.ShippingOrderExample;
import cn.itcast.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(timeout = 100000)
public class ShippingOrderServiceImpl implements ShippingOrderService {
    @Autowired
    private ShippingOrderDao shippingOrderDao;
    @Autowired
    private PackingListDao packingListDao;
    @Override
    public int deleteByPrimaryKey(String shippingOrderId) {
        ShippingOrder shippingOrder = shippingOrderDao.selectByPrimaryKey(shippingOrderId);
        if (shippingOrder.getState()==0){
            int i = shippingOrderDao.deleteByPrimaryKey(shippingOrderId);
            //1.此处根据shippingOrderId查询装箱对象,并且修改装装状态为0
            PackingList packingList = packingListDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
            packingList.setState(0l);
            //更新数据库
            packingListDao.updateByPrimaryKeySelective(packingList);
            return i;
        }
        return 0;
    }

    @Override
    public int insert(ShippingOrder record) {
        return 0;
    }

    @Override
    public int insertSelective(ShippingOrder record) {
        return 0;
    }

    @Override
    public List<ShippingOrder> selectByExample(ShippingOrderExample example) {
        return null;
    }

    @Override
    public ShippingOrder findById(String id) {
        return shippingOrderDao.selectByPrimaryKey(id);
    }

    @Override
    public ShippingOrder selectByPrimaryKey(String shippingOrderId) {
        return shippingOrderDao.selectByPrimaryKey(shippingOrderId);
    }

    @Override
    public int updateByPrimaryKeySelective(ShippingOrder record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(ShippingOrder record) {
        return 0;
    }

    @Override
    public PageInfo<ShippingOrder> findByPage(ShippingOrderExample shippingOrderExample, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ShippingOrder> list = shippingOrderDao.selectByExample(null);
        return new PageInfo<>(list);
    }

    @Override
    public void save(ShippingOrder shippingOrder) {
        shippingOrder.setState(0);

        shippingOrderDao.insertSelective(shippingOrder);
        //保存了之后要修改装箱单的状态为5
        PackingList packingList = packingListDao.selectByPrimaryKey(shippingOrder.getShippingOrderId());
        packingList.setState(5L);
        packingListDao.updateByPrimaryKeySelective(packingList);
    }
}
